﻿#region codejockey.net Copyright Banner
/***************************************************************\
 * Copyright 2010 codejockey.net.                                *
 *                                                               *
 * This code is property of codejockey.net and cannot be used    *
 * without explicit permission given by a codejockey.net         *
 * Representative.  For details contact: source@codejockey.net   *
 \***************************************************************/
#endregion

using System;
using System.IO;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using codejockey.BingPaper.Utils;

namespace codejockey.BingPaper.UnitTests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class BingUtilsTests
    {

        #region Private Constants

        private const string BaseUrl = "http://www.bing.com";
        private const string DefaultRssUrl = BaseUrl + "/HPImageArchive.aspx?format=xml&idx=0&n=1&cc=en-us";

        #endregion

        #region Private Fields

        private BingImage _image;

        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestCleanup]
        public void CleanUp()
        {
            if (_image != null)
                _image.Dispose();
        }


        [TestMethod]
        public void GetBingImageName()
        {
            var bingParams = new BingImageParams { Uri = new Uri(DefaultRssUrl) };

            var bingImage = BingUtils.GetBingImage(bingParams);

            Assert.IsFalse(string.IsNullOrEmpty(bingImage.ImageName));
            Assert.IsFalse(string.IsNullOrEmpty(bingImage.ImageUrl.ToString()));
        }

        [TestMethod]
        public void GetBingImage()
        {
            var bingParams = new BingImageParams { Uri = new Uri(DefaultRssUrl) };

            BingImage bingImage = BingUtils.GetBingImage(bingParams);

            Assert.IsFalse(bingImage.CurrentImage == null);
        }

        [TestMethod]
        public void GetCountryCode()
        {
            const string expected = "us";
            string actual = BingUtils.CurrentCountryCode;

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void GetMessagesTest()
        {
            _image = BingUtils.GetBingImage(BingUtils.CountryCodes[CountryCodes.DefaultCountryCodeKey]);
            Assert.IsTrue(_image.Messsages.Count > 0, "No messages were returned, however, at least one was expected.");
        }

        [TestMethod]
        public void GetCopyright()
        {
            // Here we assume all images will have copyright details.
            _image = BingUtils.GetBingImage(BingUtils.CountryCodes[CountryCodes.DefaultCountryCodeKey]);

            Assert.IsFalse(string.IsNullOrEmpty(_image.Copyright), "A copyright notice was not returned, however one was expected.");
        }

        [TestMethod]
        public void GetHotSpots()
        {
            _image = BingUtils.GetBingImage(BingUtils.CountryCodes[CountryCodes.DefaultCountryCodeKey]);
            Assert.IsTrue(_image.HotSpots.Count > 0, "No HotSpots were returned, however, at least one was expected.");
        }

        [TestMethod]
        public void TestParamsNullCountryCode()
        {
            var imageParams = new BingImageParams { CountryCode = string.Empty };

            _image = BingUtils.GetBingImage(imageParams);
            Assert.IsNotNull(_image.CurrentImage);
        }

        [TestMethod]
        public void TestParamsSetDayIndex()
        {
            var imageParams = new BingImageParams { DayIndex = 2 };

            _image = BingUtils.GetBingImage(imageParams);
            Assert.IsNotNull(_image.CurrentImage);
        }

        [TestMethod]
        [ExpectedException(typeof(ApplicationException))]
        public void TestInvalidDayIndexParam()
        {
            var imageParams = new BingImageParams { DayIndex = 10 };

            _image = BingUtils.GetBingImage(imageParams);
            Assert.IsNull(_image.CurrentImage);
        }

        [TestMethod]
        public void TestFileNameParam()
        {
            var imageParams = new BingImageParams();
            string filePath = Path.GetTempPath();
            string fileName = Path.GetTempFileName();

            imageParams.Path = filePath;
            imageParams.File = fileName;

            _image = BingUtils.GetBingImage(imageParams);
            Assert.AreEqual(fileName, _image.ImageFileName);
        }

        [TestMethod]
        public void TestFilePathCreation()
        {
            var imageParams = new BingImageParams();
            string filePath = string.Format(@"{0}\{1}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BingPaperUnitTestTemp");
            const string fileName = "BingPaperTemp.jpg";

            if (Directory.Exists(filePath))
                Directory.Delete(filePath, true);

            imageParams.Path = filePath;
            imageParams.File = fileName;

            _image = BingUtils.GetBingImage(imageParams);

            Directory.Delete(filePath, true);

            Assert.IsNotNull(_image.CurrentImage);
            Assert.AreEqual(Path.Combine(filePath, fileName), _image.ImageFileName);
        }
    }
}
